package com.sensePlusVR.activity.guesture;

import org.xutils.x;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sensePlusVR.activity.user.LoginActivity;
import com.sensePlusVR.base.BaseActivity;
import com.sensePlusVR.main.R;
import com.sensePlusVR.utils.AppManager;
import com.sensePlusVR.utils.Constant;
import com.sensePlusVR.utils.MyLog;
import com.sensePlusVR.utils.MyPageJump;
import com.sensePlusVR.utils.MySharedPreferences;
import com.sensePlusVR.utils.UserUtil;
import com.sensePlusVR.widget.guesture.GestureContentView;
import com.sensePlusVR.widget.guesture.GestureDrawline.GestureCallBack;


/**
 * 
 * @author: Loren
 * @create: 2016-7-20上午10:29:03
 * @desc: 手势绘制/校验界面
 *
 */
 @SuppressWarnings("unused")
@ContentView(R.layout.activity_gesture_verify)
public class GestureVerifyActivity extends BaseActivity{
	private static final String TAG = GestureVerifyActivity.class.getSimpleName();
	/** 手机号码*/
	public static final String PARAM_PHONE_NUMBER = "PARAM_PHONE_NUMBER";
	/** 意图 */
	public static final String PARAM_INTENT_CODE = "PARAM_INTENT_CODE";
	@ViewInject(R.id.top_layout)
	private RelativeLayout mTopLayout;
	@ViewInject(R.id.text_cancel)
	private TextView mTextCancel;
	@ViewInject(R.id.text_tip)
	private TextView mTextTip;
	@ViewInject(R.id.gesture_container)
	private FrameLayout mGestureContainer;
	@ViewInject(R.id.text_forget_gesture)
	private TextView mTextForget;
	private GestureContentView mGestureContentView;
	private String mParamPhoneNumber;
	private long mExitTime = 0;
	private int mParamIntentCode;
	
	private boolean isFirstReset;
	private boolean isNotReset;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	

	@Override
	public void setMyContentView() {
		x.view().inject(this);
	}
	
	@Override
	public void getIntentData() {
		super.getIntentData();
		mParamPhoneNumber = getIntent().getStringExtra(PARAM_PHONE_NUMBER);
		mParamIntentCode = getIntent().getIntExtra(PARAM_INTENT_CODE, Constant.NUMBER_0);
		isNotReset = MyPageJump.pageJumpGetBoolean(this, Constant.INTENT_GUESTURE_RESET_NOT);
		isFirstReset = MyPageJump.pageJumpGetBoolean(this, Constant.INTENT_GUESTURE_FIRST_RESET);
	}
	
	@Override
	public void initView() {
		super.initView();
		AppManager.getInstance().addActivity(this);
		
		if(isFirstReset)
		mTextTip.setText(R.string.guesture_reset_tip1);
		else
		mTextTip.setText(R.string.guesture_first_tip1);
		if(isNotReset)
		mTextTip.setText(R.string.guesture_verify_tip);
			
	}
	
	@Override
	public void initData() {
		super.initData();
		String inputCode = MySharedPreferences.getString2(GestureVerifyActivity.this,MySharedPreferences.NAME_NOT_CLEAR,Constant.SP_INPUTCODE);
		if(inputCode == ""){
			//inputCode = "1235789";
			MyLog.d(TAG, "inputCode == null");
		}
		Log.d(TAG, "input code ： " + inputCode);
		// 初始化一个显示各个点的viewGroup
		mGestureContentView = new GestureContentView(this, true, inputCode,
				new GestureCallBack() {

					@Override
					public void onGestureCodeInput(String inputCode) {}

					@Override
					public void checkedSuccess() {
						mGestureContentView.clearDrawlineState(0L);
						//MyToast.showMessage(GestureVerifyActivity.this, R.string.guesture_psw_right);
						if(isFirstReset){
							MyPageJump.pageJumpAddBoolean(GestureVerifyActivity.this, GestureEditActivity.class,Constant.INTENT_GUESTURE_FIRST_RESET,isFirstReset);
						}
						
						AppManager.getInstance().finishActivity(GestureVerifyActivity.this);
					}

					@Override
					public void checkedFail() {
							mGestureContentView.clearDrawlineState(1300L);
							mTextTip.setVisibility(View.VISIBLE);
							mTextTip.setText(R.string.guesture_psw_not_same);
							mTextTip.setTextColor(getResources().getColor(R.color.app_text_color_gray1));
							// 左右移动动画
							Animation shakeAnimation = AnimationUtils.loadAnimation(GestureVerifyActivity.this, R.anim.shake);
							mTextTip.startAnimation(shakeAnimation);
					}
				});
		// 设置手势解锁显示到哪个布局里面
		mGestureContentView.setParentView(mGestureContainer);
	
	}
	
	@Event(value = {R.id.text_forget_gesture})
	private void setViewOnclick(View view){
		switch (view.getId()) {
		case R.id.text_forget_gesture:
			UserUtil.clearnGesturePSW(this);
			  MyPageJump.pageJump(this,LoginActivity.class);
				AppManager.getInstance().finishActivity(this);
			break;
		}
	}
	
	@Override
	public void onBackPressed() {
		MyPageJump.pageJumpHome(this);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
}
